<?php

class FinancelogController extends \Base\AbstractController {

    public function listAction() {
        $request = $this->getRequest();
        $class   = $request->get('class');
        $keyword = $request->get('keyword');
        $type    = $request->get('type');
        $memberMapper = \Mapper\MemberModel::getInstance();
        $where   = [];

        if(!empty($keyword) && !empty($class)) {

            switch($class) {
                case 1  :
                    $first = ['uid' => $keyword];
                    break;
                case 2 :
                    $memberModel = $memberMapper->fetch(['nick_name LIKE "%' . urlencode($keyword) . '%"']);
                    if($memberModel instanceof MemberModel) {
                        $where['uid'] = $memberModel->getId();
                    }
                    break;
                case 3 :
                    $memberModel = $memberMapper->fetch(['mobile' => $keyword]);
                    if($memberModel instanceof MemberModel) {
                        $where['uid'] = $memberModel->getId();
                    }
                    break;
                default :
                    break;
            }
        }

        if(!empty($type)) {
            $second = ['type' => $type];
        }

        //组装查询语句
        if(!empty($first) && !empty($second)) {
            $where = array_merge($first, $second);
        } elseif(!empty($first) || !empty($second)) {
            !empty($first) ? $where = $first : $where = $second;
        }


        $memberMapper = Mapper\MemberModel::getInstance();

        $page = new \Lib\Paginator('financeLog');
        $page->where($where);
        $this->assign('count', $page->count());
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $models = $page->lists();

        foreach($models as $model) {
            $data = $model->toArray();

            $member                   = $memberMapper->findById($data['uid']);
            $data['member_nick_name'] = urldecode($member->getNick_name());
            $return[]                 = $data;
        }



        $this->assign('lists', $return);
    }

}
